<?php

	// +----------------------------------------------------------------------
	// | 店铺管理
	// +----------------------------------------------------------------------
	// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
	// +----------------------------------------------------------------------
	// | Author: 微尘 <yicmf@qq.com>
	// +----------------------------------------------------------------------

	namespace app\controller\store\admin;

	use app\event\file\Attachment as AttachmentEvent;
    use app\model\pos\Pos as PosModel;
    use app\model\store\Good as GoodModel;
	use app\event\store\Good as GoodEvent;
	use app\model\store\Template as TemplateModel;
    use app\model\ucenter\User as UserModel;
    use think\facade\Config;
	use yicmf\builder\Edit;
	use yicmf\builder\Table;

	class Good extends Common
	{
		protected function initialize()
		{
			parent::initialize();
		}

		public function _filter()
		{
            $map[] = ['type', 'in', [0,1]];
			return $map;
		}

		/**
		 * 商品评论
		 * @return mixed|string
		 * @access public
		 * @author 微尘 <yicmf@qq.com>
		 */
		public function index()
		{
			// ID 商户编号 商户名称 联系人 联系电话  支付公司  状态 合伙人 入网时间 序列号
            //状态：正常 审核失败 注销 等

			$builder = new Table();
			$builder->model(GoodModel::class)
				->where($this->_filter())
				->order('id DESC')
                ->buttonUpdate()
//				->suggest('超级管理员可以通过点击店铺昵称，直接登录商家后台')
				->searchText('title', '标题', '')
//				             ->buttonUpdate('update','同步商户信息')
				//             ->searchSelect('status', '状态',$statusOptions)
				->keyId()
//				->keyHidden('code')
//				->keyLink('nickname', '店铺昵称', '/store/{{d.code}}', '_block')

				->keyText('title', '标题')
				->keyImageModel('cover_id', '封面')
				->keyDecimal('price', '价格')
				->keyStatus()
				->actionUpdate()
				->actionForbid()
				->actionRestore();
			return $builder->fetch();
		}

        /**
         * 文档更新页面初始化.
         */
        public function update()
        {
            if ($this->request->isPost()) {
                // 验证数据
                if ($this->request->param('id')) {
                    $data = GoodModel::where('id', $this->request->param('id'))->find();
                    $data->save($this->request->param());
                    $this->_returnSuccess('更新成功');
                } else {
                    GoodModel::create($this->request->param());
                    $this->_returnSuccess('新增成功');
                }
            } else {
                if ($this->request->has('id', 'get')) {

                    $data = GoodModel::where('id', $this->request->get('id'))->find();
                } else {
                    // 创建的默认值
                    $data =[];
                }

                // 获取表单字段
                $builder = new Edit();
                $builder->title('商品');
                $builder
                    ->keyText('title', '标题')
                    ->keyImageModel('cover_id','封面')
                    ->keyImageModel('image_id','内图')
                    ->keyNumber('stock','库存')
                    ->keyDecimal('price','单价')
                    ->keyEditor('body', '内容')
                    ->data($data)
                    ->buttonSubmit();
                return $builder->fetch();
            }
        }

        /**
         * 删除消息
         */
        public function delete()
        {
            $message = DocumentModel::where('id', $this->request->param('id'))->find();
            if ($message) {
                $message['status'] = -2;
                $message->save();
                $this->_returnSuccess('删除成功');
            } else {
                $this->_returnSuccess('操作失败');
            }
        }


		/**
		 * 删除消息
		 */
		public function forbid()
		{
			$message = GoodModel::where('id', $this->request->param('id'))
				->find();
			if ($message) {
				$message['status'] = -1;
				$message->save();
				$this->_returnSuccess( '禁用成功');
			} else {
				$this->_returnSuccess( '禁用失败');
			}
		}
		/**
		 * 删除消息
		 */
		public function restore()
		{
			$message = GoodModel::where('id', $this->request->param('id'))
				->find();
			if ($message) {
				$message['status'] = 1;
				$message->save();
				$this->_returnSuccess( '恢复成功');
			} else {
				$this->_returnSuccess( '恢复失败');
			}
		}
	}
